package com.guochuang.yunwang.promanager.controller;

import com.guochuang.yunwang.promanager.annotation.CancelLoginIntercept;
import com.guochuang.yunwang.promanager.domain.UserGroup;
import com.guochuang.yunwang.promanager.service.UserGroupService;
import com.guochuang.yunwang.promanager.utils.ProjectConstant;
import com.guochuang.yunwang.promanager.utils.ResultEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Slf4j
@Api(value = "用户团队接口模块",tags = "用户团队接口模块")
@RestController
public class UserGroupController {
    @Autowired
    private UserGroupService userGroupService;

    @Autowired
    private SimpleDateFormat simpleDateFormat;


    @PutMapping(value = "/update/group/user/{uid}/{gid}/{rid}")
    public ResultEntity updateUserRole(@PathVariable Integer uid,
                                       @PathVariable Integer gid,
                                       @PathVariable Integer rid){
        Integer nums = userGroupService.updateGroupUserRole(uid,gid,rid);

        if(nums>0){
            return ResultEntity.successWithoutData();
        }else{
            return ResultEntity.failed(ProjectConstant.DELETE_USER_FAILED);
        }
    }

    @DeleteMapping(value = "/delete/group/user/{gid}/{uid}")
    public ResultEntity deleteGroupUser(@PathVariable Integer gid,
                                        @PathVariable Integer uid){

        Integer nums = userGroupService.deleteGroupUser(gid,uid);

        if(nums>0){
            return ResultEntity.successWithoutData();
        }else{
            return ResultEntity.failed(ProjectConstant.DELETE_USER_FAILED);
        }
    }


    @PutMapping(value = "/update/group/users/{gid}")
    @ApiOperation(value = "修改团队中的人员")
    public ResultEntity addUserToGroup(@PathVariable Integer gid,
                                       @RequestBody List<Integer> uids){

        Integer nums = userGroupService.addUserToGroup(gid,uids);

        if(nums>0){
            return ResultEntity.successWithoutData();
        }else{
            return ResultEntity.failed(ProjectConstant.GROUP_ADD_USER_FAILED);
        }
    }

    @PostMapping(value = "/add/usergroup")
    @ApiOperation(value = "添加用户团队关联信息")
    @CancelLoginIntercept
    public ResultEntity addUserGroup(@RequestBody UserGroup userGroup){
        String now = simpleDateFormat.format(new Date());
        userGroup.setGmtCreate(now);
        userGroup.setGmtModified(now);

        Integer res = userGroupService.addUserGroup(userGroup);
        if (res > 0){
            return ResultEntity.successWithoutData();
        }
        else{
            return ResultEntity.failed(ProjectConstant.USER_ADD_FAILED);
        }
    }

    @PutMapping(value = "/update/usergroup")
    @ApiOperation(value = "更新用户团队关联信息")
    @CancelLoginIntercept
    public ResultEntity updateUserGroup(@RequestBody UserGroup userGroup){
        String now = simpleDateFormat.format(new Date());
        userGroup.setGmtModified(now);

        Integer res = userGroupService.updateUserGroup(userGroup);
        if (res > 0){
            return ResultEntity.successWithoutData();
        }
        else {
            return ResultEntity.failed(ProjectConstant.USER_CHANGE_FAILED);
        }
    }

    @GetMapping(value = "/get/rids/by/gid")
    @ApiOperation(value = "通过团队id获取所有的角色id")
    @CancelLoginIntercept
    public ResultEntity getAllRidsByGid(Integer gid){
        List<Integer> res = userGroupService.selectAllRidsByGid(gid);

        if (res != null){
            return ResultEntity.successWithData(res);
        }
        else {
            return ResultEntity.failed(ProjectConstant.USER_FAILED_MESSAGE);
        }
    }
}


